package com.huiquan.vocab.controller;

import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.vocab.service.VocabElementClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class VocabElementClassifyController {

	@Autowired
	private VocabElementClassifyService vocabElementClassifyService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/vocab/element_classify_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("exactSearchFlag", "0");
			pageParam.put("checkFlag", "0");

			// 获取所有的可分类词语列表
			pageParam.put("parentData", vocabElementClassifyService.getParentData());

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		// 获取页面参数，如果request中有值则修改session，否则从session中拿值
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String nameKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "nameKey");
		String checkFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "checkFlag");
		String categoryKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "categoryKey");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");
		String parentKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "parentKey");

		return vocabElementClassifyService.list(start, nameKey, exactSearchFlag, checkFlag, categoryKey,countPerPageStr,parentKey);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_classify_init.do")
	public ReturnData init(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		return vocabElementClassifyService.init();
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_add_category.do")
	public ReturnData addCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name").trim();

		return vocabElementClassifyService.addCategory(name);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_delete_category.do")
	public ReturnData deleteCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name").trim();

		return vocabElementClassifyService.deleteCategory(name);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_change_category.do")
	public ReturnData changeCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String category = req.getParameter("category").trim();

		return vocabElementClassifyService.changeCategory(id, category);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_change_parent.do")
	public ReturnData changeParent(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String parent = req.getParameter("parent").trim();

		return vocabElementClassifyService.changeParent(id, parent);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_classify_submit_page.do")
	public ReturnData submitPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String ids = req.getParameter("ids");

		return vocabElementClassifyService.submitPage(ids);
	}

	@RequestMapping(value = "/vocab/element_classify_export_parent.htm")
	public void exportParent(HttpServletRequest req, HttpServletResponse resp) throws Exception {

		vocabElementClassifyService.exportParent(resp);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_classify_import_parent.do")
	public ReturnData importParent(HttpServletRequest req, @RequestParam(required = false) MultipartFile file)
			throws Exception {

		return vocabElementClassifyService.importParent(file);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_classify_delete.do")
	public ReturnData deleteClassify(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");

		return vocabElementClassifyService.deleteClassify(id);
	}
}
